package com.schuxl.htmlobserver;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

import com.schuxl.htmlobserver.dataobject.schemaTypen.sites.Sites;
import com.schuxl.htmlobserver.dataobject.schemaTypen.sites.Sites.Site;
import com.schuxl.htmlobserver.observer.Autoscout24HtmlObserver;
import com.schuxl.htmlobserver.observer.MobileHtmlObserver;
import com.schuxl.htmlobserver.observer.TausendPsObserver;
import com.schuxl.htmlobserver.observer.WillhabenHtmlObserver;




public class StartHtmlObserver {

	
	public enum DOMAIN {
		WILLHABEN {public String toString() {return "willhaben.at";} },
		TAUSENDPS {public String toString() {return "1000ps.at";} },
		EBAY {public String toString() {return "ebay.at";} }, 
		BAZAR {public String toString() {return "bazar.at";} }, 
		MOBILE {public String toString() {return "mobile.de";} },
//		AUTOSCOUT24 {public String toString() {return "autoscout24";} }
	}
	
	//Linux
//	-Dcom.schuxl.htmlobserver.contextpath=/home/schuxl/programme/eclipse/standard/workspace/HtmlObserver/
//	-Dcom.schuxl.htmlobserver.config=config/config.properties
//	-Dcom.schuxl.htmlobserver.log4jConfigFile=config/log4j.xml
	
	//Windows
//	-Dcom.schuxl.htmlobserver.contextpath=D:/Schuxl/dev/workspace/HtmlObserver/
//	-Dcom.schuxl.htmlobserver.config=config/config.properties
//	-Dcom.schuxl.htmlobserver.log4jConfigFile=config/log4j.xml
	
	
	private static Logger LOGGER = null;
	public static Properties PROPERTIES = new Properties(); 

	
	static {
		String contextPath = System.getProperty("com.schuxl.htmlobserver.contextpath");
		String configFile = System.getProperty("com.schuxl.htmlobserver.config");
		String log4jConfigFile = System.getProperty("com.schuxl.htmlobserver.log4jConfigFile");
		DOMConfigurator.configureAndWatch(contextPath+log4jConfigFile, 60*1000 );
		LOGGER = Logger.getLogger(StartHtmlObserver.class);
		LOGGER.debug("Beginne Einlesen von config.properties");
		try {
			PROPERTIES.load(new FileInputStream(contextPath+configFile));
		} catch (Exception e) {
			LOGGER.error("Fehler beim Einlesen von config.properties. "+e);
		}
	}
	
	public static void main(String[] args) {
		try {
			LOGGER.debug("Beginne Einlesen von ObservedSites.xml");
			JAXBContext jaxbContext = JAXBContext.newInstance("com.schuxl.htmlobserver.dataobject.schemaTypen.sites");
			Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
		    Sites sites = (Sites)unmarshaller.unmarshal(new File(StartHtmlObserver.PROPERTIES.getProperty("com.schuxl.htmlobserver.observedSitesFile")));
		    
		    for(Site site : sites.getSite()) {
		    	if(site.getUrl().contains(StartHtmlObserver.DOMAIN.WILLHABEN.toString())) {
		    		LOGGER.debug("Starte Willhaben-HtmlObserver Thread");
			    	new Thread(new WillhabenHtmlObserver(site)).start();
		    	}
		    	else if(site.getUrl().contains(StartHtmlObserver.DOMAIN.TAUSENDPS.toString())) {
		    		LOGGER.debug("Starte TausendPs-HtmlObserver Thread");
			    	new Thread(new TausendPsObserver(site)).start();
		    	}
		    	else if(site.getUrl().contains(StartHtmlObserver.DOMAIN.MOBILE.toString())) {
		    		LOGGER.debug("Starte Mobile-HtmlObserver Thread");
			    	new Thread(new MobileHtmlObserver(site)).start();
		    	}
//		    	else if(site.getUrl().contains(StartHtmlObserver.DOMAIN.AUTOSCOUT24.toString())) {
//		    		LOGGER.debug("Starte Autoscout24-HtmlObserver Thread");
//			    	new Thread(new Autoscout24HtmlObserver(site)).start();
//		    	} //Zum proxy debugen (nc -l -p 8888)
		    	else if(site.getUrl().contains("localhost")) {
		    		LOGGER.debug("Starte Autoscout24-HtmlObserver Thread");
			    	new Thread(new Autoscout24HtmlObserver(site)).start();
		    	}
		    }
		} catch (Exception e) {
			LOGGER.error("Fehler beim Starten! ", e);
		}
	}
	
	
	
}
